#include "Mesh.h"
#include "ElementBuffer.h"
#include "VertexBuffer.h"
#include "Context.h"
#include "Shader.h"
#include <glm/gtc/type_ptr.hpp>
#include "Material.h"

Mesh::Mesh(Context* context) :Component(context)
{

}

Mesh::~Mesh()
{
	
}
void Mesh::GetBatch(std::vector<Batch3D>& batches)
{
	Batch3D batch(gameObject_, material_.get(), mode_, elementBuffer_.get(), &vertexAttributList_);
	batch.start = start_;
	batch.count = count_;
	batches.emplace_back(batch);
}

void Mesh::SetElementBuffer(std::shared_ptr<ElementBuffer> elem)
{
	if (elem != elementBuffer_)
	{
		elementBuffer_.swap(elem);
	}
}

void Mesh::AddVertextAttribut(const VertextAttribut & attr)
{
	vertexAttributList_.push_back(attr);
}

void Mesh::SetMaterial(std::shared_ptr<Material> mat)
{
	if (mat == material_)
	{
		return;
	}

	if (mat)
	{
		material_ .swap(mat);
	}
}

std::shared_ptr<Component> Mesh::Clone()
{
	Mesh* temp = new Mesh(context_);
	temp->SetElementBuffer(elementBuffer_);
	temp->SetMaterial(material_);
	temp->SetMode(mode_);
	temp->SetRang(start_, count_);

	for (size_t i = 0; i < vertexAttributList_.size(); i++)
	{
		temp->AddVertextAttribut(vertexAttributList_[i]);
	}

	return std::shared_ptr<Component>(temp);
}

